import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/Home' // 真实路径是 / 定位到 /Home
  }, {
    path: '/Home',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    beforeEnter: (to, from, next) => {
      // ...
      if (!localStorage.getItem('token')) {
        next('/login')
      } else {
        next()
      }
    }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/register.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue'),
    beforeEnter: (to, from, next) => {
      if (localStorage.getItem('token')) {
        next('/')
      } else {
        next()
      }
    }
  },
]

const router = new VueRouter({
  routes
})

export default router